package spring.context;

import spring.exceptions.FactoryErrorException;
import spring.transaction.TransactionBeanFactory;

import java.util.Objects;

public final class ApplicationContextHolder {

    private static final ThreadLocal<TransactionBeanFactory> FACTORY_LOCAL = new ThreadLocal<>();

    private ApplicationContextHolder() {
    }

    public static void register(TransactionBeanFactory beanFactory) {
        FACTORY_LOCAL.set(beanFactory);
    }

    public static TransactionBeanFactory getBeanFactory() {
        TransactionBeanFactory factory = FACTORY_LOCAL.get();
        if (Objects.isNull(factory)) {
            throw new FactoryErrorException("bean 工厂对象不存在");
        }
        return factory;
    }
}
